// JSHumanizer, version 0.0
// Copyright © 2010, Richard Cook.
// http://jshumanizer.googlecode.com/
//
// This file is part of JSHumanizer.
 //
// JSHumanizer is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the licence or
// (at your option) any later version.
//
// JSHumanizer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with JSHumanizer. If not, see <http://www.gnu.org/licenses/>.

var JSHumanizer = null;
var Tester = (function(self) {

  var assert_count = 0;
  var failed_assert_count = 0;
  var succeeded_assert_count = 0;

  self.loadJSHumanizer = function(language) {
    JSHumanizer = null;
    load("jshumanizer.js");
    load("jshumanizer." + language + ".js");
  };

  self.assertEqual = function(expected_value, actual_value) {
    ++assert_count;
    if (expected_value == actual_value) {
      ++succeeded_assert_count;
    }
    else {
      print("Assert failed: \"" + expected_value + "\" != \"" + actual_value + "\"");
      ++failed_assert_count;
    }
  };

  self.displayBanner = function() {
    self.loadJSHumanizer("en-GB");
    print();
    print("JSHumanizer, version 0.0: test suite");
    print("Copyright © 2010, Richard Cook.");
    print("http://jshumanizer.googlecode.com/");
    print();
    print("This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you");
    print("are welcome to redistribute it. It is distributed under the terms of the GNU");
    print("General Public License. A copy of this licence is provided in the file COPYING.");
    print();
  };

  self.displayReport = function() {
    self.loadJSHumanizer("en-GB");
    var failed_percent = failed_assert_count / assert_count * 100;
    var succeeded_percent = succeeded_assert_count / assert_count * 100;
    print("TEST SUMMARY");
    print("  Total asserts     : " + JSHumanizer.humanizeInt(assert_count));
    print("  Failed asserts    : " +
      JSHumanizer.humanizeInt(failed_assert_count) + " (" +
      JSHumanizer.humanizeDecimal(failed_percent, 1) + "%)");
    print("  Successful asserts: " +
      JSHumanizer.humanizeInt(succeeded_assert_count) + " (" +
      JSHumanizer.humanizeDecimal(succeeded_percent, 1) + "%)");
    print();
  };

  return self;
}(Tester || {}));

Tester.displayBanner();

Tester.loadJSHumanizer("de");
Tester.assertEqual(
  "Freitag 9. Juli 1982 um 11:22:33",
  JSHumanizer.getFullDateTimeString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "Freitag 9. Juli 1982",
  JSHumanizer.getFullDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "9. Juli 1982",
  JSHumanizer.getLongDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "9.7.1982",
  JSHumanizer.getCommonDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "09.07.82",
  JSHumanizer.getShortDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "1.234,56",
  JSHumanizer.humanizeDecimal(1234.563, 2)
);
Tester.assertEqual(
  "1.234,56",
  JSHumanizer.humanizeDecimal(1234.564, 2)
);
Tester.assertEqual(
  "1.234,57",
  JSHumanizer.humanizeDecimal(1234.565, 2)
);
Tester.assertEqual(
  "1.234,57",
  JSHumanizer.humanizeDecimal(1234.566, 2)
);

Tester.loadJSHumanizer("en-GB");
Tester.assertEqual(
  "Friday 9 July 1982 at 11:22:33",
  JSHumanizer.getFullDateTimeString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "Friday 9 July 1982",
  JSHumanizer.getFullDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "9 July 1982",
  JSHumanizer.getLongDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "9/7/1982",
  JSHumanizer.getCommonDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "09.07.82",
  JSHumanizer.getShortDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "1,234.56",
  JSHumanizer.humanizeDecimal(1234.563, 2)
);
Tester.assertEqual(
  "1,234.56",
  JSHumanizer.humanizeDecimal(1234.564, 2)
);
Tester.assertEqual(
  "1,234.57",
  JSHumanizer.humanizeDecimal(1234.565, 2)
);
Tester.assertEqual(
  "1,234.57",
  JSHumanizer.humanizeDecimal(1234.566, 2)
);

Tester.loadJSHumanizer("en-US");
Tester.assertEqual(
  "Friday, July 9, 1982 at 11:22:33",
  JSHumanizer.getFullDateTimeString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "Friday, July 9, 1982",
  JSHumanizer.getFullDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "July 9, 1982",
  JSHumanizer.getLongDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "7/9/1982",
  JSHumanizer.getCommonDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "07.09.82",
  JSHumanizer.getShortDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "1,234.56",
  JSHumanizer.humanizeDecimal(1234.563, 2)
);
Tester.assertEqual(
  "1,234.56",
  JSHumanizer.humanizeDecimal(1234.564, 2)
);
Tester.assertEqual(
  "1,234.57",
  JSHumanizer.humanizeDecimal(1234.565, 2)
);
Tester.assertEqual(
  "1,234.57",
  JSHumanizer.humanizeDecimal(1234.566, 2)
);

Tester.loadJSHumanizer("fr");
Tester.assertEqual(
  "vendredi 9 juillet 1982 à 11:22:33",
  JSHumanizer.getFullDateTimeString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "vendredi 9 juillet 1982",
  JSHumanizer.getFullDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "9 juillet 1982",
  JSHumanizer.getLongDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "09/07/1982",
  JSHumanizer.getCommonDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "09/07/82",
  JSHumanizer.getShortDateString(new Date(1982, 6, 9, 11, 22, 33))
);
Tester.assertEqual(
  "1&nbsp;234,56",
  JSHumanizer.humanizeDecimal(1234.563, 2)
);
Tester.assertEqual(
  "1&nbsp;234,56",
  JSHumanizer.humanizeDecimal(1234.564, 2)
);
Tester.assertEqual(
  "1&nbsp;234,57",
  JSHumanizer.humanizeDecimal(1234.565, 2)
);
Tester.assertEqual(
  "1&nbsp;234,57",
  JSHumanizer.humanizeDecimal(1234.566, 2)
);

Tester.displayReport();

